package com.jimmy.dreamer;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import static android.provider.BaseColumns._ID;
import static com.jimmy.dreamer.DbConstants1.DREAM;
import static com.jimmy.dreamer.DbConstants1.TABLE_NAME;
import static com.jimmy.dreamer.Dreamer.isLogin;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ViewDream extends Activity implements OnClickListener{
	private DbHelper1 dbhelper = null;

	private TextView textArea =null;
    private Button btnAdd = null;
    private Button btnBack = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewdream);
		
        initView();
        openDatabase();
        showDream();
	}
	@Override
    protected void onDestroy() {
        super.onDestroy();
        closeDatabase();
    }
    private void openDatabase(){
        dbhelper = new DbHelper1(this); 
    }

    private void closeDatabase(){
        dbhelper.close();
    }

    private void initView(){
    	textArea = (TextView) findViewById(R.id.tv_dreamContent);
        btnAdd = (Button) findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(this);
        btnBack = (Button) findViewById(R.id.btn_back);
        btnBack.setOnClickListener(this);
    }
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_add:
        	Intent r = new Intent(this, Record.class);
    		startActivity(r);
            break;
            
        case R.id.btn_back:
        	Intent d = new Intent(ViewDream.this, Dreamer.class);
    		startActivity(d);

        default:
        }
    }
    private Cursor getCursor(){
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        String[] columns = {_ID, DREAM};

        Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null, null);
        startManagingCursor(cursor);

        return cursor;
    }
    private void showDream(){
    	Cursor cursor = getCursor();
    	StringBuilder resultData = new StringBuilder("RECORD: \n");
 
    	while(cursor.moveToNext()){
    		String d = cursor.getString(1);
    		
    		resultData.append(d);
    		resultData.append("\n");
    	}
    	textArea.setText(resultData);
    }
}
